home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 25 / Mac Magazin and MacEasy Magazine CD - Issue 25.iso / Online / Joe's-PPP-Scripts / PPP Connection / •PPP connection (text) < prev    next >
Text File  |  1996-08-20  |  8KB  |  288 lines

  1.  
  2. property openFile : {""}
  3. property closeFile : {""}
  4. property forwardFile : {""}
  5. property ftp_directory : {""}
  6.  
  7. global transferFile, parsedFile, useDirectory, serverResponse
  8. global LF, CR, CRLF
  9. global intermediateResult, fileName, sss
  10. global newsockets, socketsList
  11.  
  12. on run
  13.     
  14.     if openFile = {""} then
  15.         set dialogText to {"Select a HTML file to be sent at OPEN connection:"}
  16.         getFilelocation(dialogText)
  17.         set openFile to result
  18.     end if
  19.     if closeFile = {""} then
  20.         set dialogText to {"Select a HTML file to be sent at CLOSE connection:"}
  21.         getFilelocation(dialogText)
  22.         set closeFile to result
  23.     end if
  24.     
  25.     if ftp_directory = {""} then
  26.         set dialog_response to (display dialog "Enter the directory to use for the HTML files:" default answer "www_docs" buttons "OK" default button "OK")
  27.         
  28.         set ftp_directory to text returned of dialog_response
  29.         
  30.     end if
  31.     
  32.     if forwardFile = {""} then
  33.         set dialogText to {"Select an E-mail \".forward\" file:"}
  34.         getFilelocation(dialogText)
  35.         set forwardFile to result
  36.     end if
  37.     
  38.     openPPP (getPPPServer)
  39.     repeat while not (PPPopened)
  40.     end repeat
  41.     checkMail()
  42.     set transferFile to {""}
  43.     open for access file openFile
  44.     set transferFile to (read openFile)
  45.     close access openFile
  46.     dealwithFile()
  47.     set fileName to "currentIP.html"
  48.     set useDirectory to true
  49.     ftpPlanfile()
  50.     
  51.     set transferFile to {""}
  52.     open for access file forwardFile
  53.     set transferFile to (read forwardFile)
  54.     close access forwardFile
  55.     dealwithFile()
  56.     set fileName to ".forward"
  57.     set useDirectory to false
  58.     ftpPlanfile()
  59.     
  60.     set LF to ASCII character (10)
  61.     set socketsList to {}
  62.     repeat with x from 1 to 2
  63.         set newSocket to {tcp wait for connect port 666}
  64.         set socketsList to socketsList & newSocket
  65.     end repeat
  66.     
  67. end run
  68.  
  69. on checkMail()
  70.     set mmm to (tcp connect to host (GetICPreference SMTP host) port 110)
  71.     try
  72.         readresponse(mmm)
  73.         tcp write data "USER " & (GetICPreference mail account) & return stream mmm using ISO88591
  74.         readresponse(mmm)
  75.         tcp write data "PASS " & (GetICPreference mail password) & return stream mmm using ISO88591
  76.         readresponse(mmm)
  77.         tcp write data "STAT" & return stream mmm using ISO88591
  78.         readresponse(mmm)
  79.         set oldDelimiters to AppleScript's text item delimiters
  80.         set AppleScript's text item delimiters to {" "}
  81.         set messageCount to text item 2 of serverResponse as integer
  82.         set AppleScript's text item delimiters to oldDelimiters
  83.         if messageCount ≠ 0 then
  84.             if messageCount = 1 then
  85.                 speak "There is one message"
  86.             else
  87.                 speak "There are " & messageCount & " new messages"
  88.             end if
  89.         else
  90.             speak "There are no messages"
  91.         end if
  92.         tcp close stream mmm
  93.     on error
  94.         display dialog "Error with mail check" buttons {"OK"} default button "OK"
  95.     end try
  96. end checkMail
  97.  
  98. on getFilelocation(dialogText)
  99.     try
  100.         set fileLocation to (choose file with prompt ¬
  101.             dialogText of type {"TEXT"}) as string
  102.         
  103.     on error errMsg
  104.         abort()
  105.     end try
  106. end getFilelocation
  107.  
  108. on dealwithFile()
  109.     try
  110.         searchReplace of me into transferFile at "%ip%" given replaceString:(tcp my address)
  111.         set intermediateResult to result as text
  112.     on error errMsg
  113.         abort()
  114.     end try
  115.     try
  116.         searchReplace of me into intermediateResult at "%clock%" given replaceString:(current date)
  117.         set parsedFile to result as text
  118.     on error errMsg
  119.         abort()
  120.     end try
  121. end dealwithFile
  122.  
  123. on searchReplace into mainString at searchString given replaceString:replaceString
  124.     
  125.     repeat while mainString contains searchString
  126.         set foundOffset to offset of searchString in mainString
  127.         set stringStart to text 1 thru (foundOffset - 1) of mainString
  128.         set stringEnd to text (foundOffset + (count of searchString)) thru -1 of mainString
  129.         set mainString to stringStart & replaceString & stringEnd
  130.     end repeat
  131.     return mainString
  132. end searchReplace
  133.  
  134. on ftpPlanfile()
  135.     set LF to (ASCII character of 10)
  136.     set CR to (ASCII character of 13)
  137.     set CRLF to CR & LF
  138.     try
  139.         set oldDelimiters to AppleScript's text item delimiters
  140.         set AppleScript's text item delimiters to {"."}
  141.         set localAddr to (tcp my address)
  142.         set localAddrList to text items of localAddr
  143.         set AppleScript's text item delimiters to {","}
  144.         set localAddrCommaString to localAddrList as string
  145.         set AppleScript's text item delimiters to oldDelimiters
  146.         set sss to (tcp connect to host (GetICPreference ftp host) port 21)
  147.         try
  148.             readresponse(sss)
  149.             
  150.             tcp write stream sss data "USER " & (GetICPreference mail account) & return using ISO88591
  151.             readresponse(sss)
  152.             
  153.             tcp write stream sss data "PASS " & (GetICPreference mail password) & return using ISO88591
  154.             readresponse(sss)
  155.             
  156.             if useDirectory = true then
  157.                 tcp write stream sss data "CWD " & ftp_directory & return using ISO88591
  158.                 readresponse(sss)
  159.             end if
  160.             
  161.             tcp write stream sss data "TYPE A" & return using ISO88591
  162.             readresponse(sss)
  163.             
  164.             set data_stream to (tcp wait for connect)
  165.             set data_stream_status to (tcp status stream data_stream)
  166.             set portHiByte to round (local port of data_stream_status) / 256 rounding down
  167.             set portLoByte to (local port of data_stream_status) mod 256
  168.             
  169.             tcp write stream sss data "PORT " & localAddrCommaString & "," & portHiByte & "," & portLoByte & return using ISO88591
  170.             readresponse(sss)
  171.             
  172.             tcp write stream sss data "STOR " & fileName & return & LF
  173.             readresponse(sss)
  174.             
  175.             repeat until (connection status of (tcp status stream data_stream) = Connected)
  176.             end repeat
  177.             tcp write stream data_stream data parsedFile using ISO88591
  178.             
  179.             try
  180.             on error msg number num from obj partial result pr
  181.                 tcp close stream data_stream
  182.                 error msg number num from data_stream
  183.             end try
  184.             tcp close stream data_stream
  185.             readresponse(sss)
  186.             tcp write stream sss data "QUIT" & return using ISO88591
  187.             readresponse(sss)
  188.             tcp close stream sss
  189.             
  190.         on error msg number num from obj partial result pr
  191.             try
  192.                 tcp write stream sss data "QUIT" & return using ISO88591
  193.                 readresponse(sss)
  194.             on error
  195.             end try
  196.             
  197.             tcp close stream sss
  198.             error msg number num from obj partial result pr
  199.         end try
  200.     on error errMsg
  201.         abort()
  202.     end try
  203. end ftpPlanfile
  204.  
  205. on readresponse(sstream)
  206.     set LF to ASCII character (10)
  207.     repeat until (tcp ahead characters LF stream sstream)
  208.     end repeat
  209.     set serverResponse to (tcp read until characters LF stream sstream using ISO88591)
  210. end readresponse
  211.  
  212. on idle
  213.     if (PPPopened) then
  214.         set newsockets to {}
  215.         repeat with x from 1 to (length of socketsList)
  216.             set s to item x of socketsList
  217.             try
  218.                 set itsStatus to (tcp status stream s)
  219.                 if connection status of itsStatus = Connected then
  220.                     set newsockets to newsockets & {tcp wait for connect port 666}
  221.                     handleConnection(s)
  222.                 else
  223.                     set newsockets to newsockets & {s}
  224.                 end if
  225.             on error
  226.             end try
  227.         end repeat
  228.         set socketsList to newsockets
  229.         set newsockets to {}
  230.         
  231.         return 10
  232.     else
  233.         quit
  234.     end if
  235. end idle
  236.  
  237. on handleConnection(s)
  238.     try
  239.         speak "There is new E-mail"
  240.         tcp close stream s
  241.     on error
  242.         try
  243.             tcp close stream s
  244.         on error
  245.         end try
  246.     end try
  247. end handleConnection
  248.  
  249. on abort()
  250.     display dialog "Error. Script aborted" buttons {"OK"} default button "OK"
  251.     set openFile to {""}
  252.     set closeFile to {""}
  253.     set forwardFile to {""}
  254.     set ftp_directory to {""}
  255.     try
  256.         tcp close stream sss
  257.     on error
  258.     end try
  259.     quit
  260. end abort
  261.  
  262. on quit
  263.     try
  264.         repeat with x from 1 to (length of socketsList)
  265.             set s to item x of socketsList
  266.             try
  267.                 tcp close stream s
  268.             on error
  269.             end try
  270.         end repeat
  271.         
  272.         if (PPPopened) then
  273.             set transferFile to {""}
  274.             open for access file closeFile
  275.             set transferFile to (read closeFile)
  276.             close access closeFile
  277.             dealwithFile()
  278.             set fileName to "currentIP.html"
  279.             set useDirectory to true
  280.             ftpPlanfile()
  281.             
  282.             closePPP with hard close
  283.             
  284.         end if
  285.     on error
  286.     end try
  287.     continue quit
  288. end quit